import { request } from '../utils/request'

async function getLineData(query = {}) {
  const { data } = await request.get('/linedata/getLineData', { params: { ...query } })
  return data
}

async function getSectionData(query = {}) {
  const { data } = await request.get('/linedata/getSectionData', { params: { ...query } })
  return data
}

async function getStationNames() {
  const { data } = await request.get('/station/getNames')
  return data
}

async function getOrganizationInfoList() {
  const { data } = await request.get('/organization/getInfoList')
  return data
}

async function setOrgOptions() {
  const res = await getOrganizationInfoList()
    if (res.code === 0) {
      const idMapping = res.data.reduce((acc, el, i) => {
        acc[el.orgId] = i
        return acc
      }, {})
      let root = null
      res.data.forEach(el => {
// 判断根节点
        if (el.orgPid === 0) {
            root = el
            return
        }
// 用映射表找到父元素
        var parentEl = res.data[idMapping[el.orgPid]]
// 把当前元素添加到父元素的`children`数组中
        parentEl.children = [...(parentEl.children || []), el]
        })
        return root
    } else {
      return null
    }
}
export { getLineData, getSectionData, getStationNames, setOrgOptions }
